/*
 * Created on Oct 12, 2005
 *
 */
package littlecyberwar.ui;

import javax.swing.*;
import java.util.*;
//import littlecyberwar.util.*;
import littlecyberwar.tool.*;
import littlecyberwar.model.*;
import java.awt.event.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ChangeLayerDialog extends JDialog {
	private static ChangeLayerDialog instance;
	
	private static ButtonGroup group;
	private JPanel selections;
	private JPanel overallPanel;
	private int numOfAttrib;
	InternalCmdFacade mod;

	private ChangeLayerDialog() {
		selections = new JPanel();
		overallPanel = new JPanel();
		overallPanel.setLayout(new BoxLayout(overallPanel, BoxLayout.PAGE_AXIS));

		selections.setLayout(new SpringLayout());
		group = new ButtonGroup();
		numOfAttrib = 0;
		this.setTitle("Select layer to move selected units to");
	}
	
	public void addEntry( String layerName, String indexName ){
		JRadioButton radioBut = new JRadioButton(layerName);
		radioBut.setActionCommand(indexName);
		group.add( radioBut );
		selections.add(radioBut);
		numOfAttrib ++;
		return;		
	}
	
	public static ChangeLayerDialog getInstance() {
		if ( instance == null ){
			instance = new ChangeLayerDialog();
		}
		return ( instance );
	}
	
	/**
	 * Create the dialog from all the entries added 
	 *
	 */
	public void compose(InternalCmdFacade in ) {
		SpringUtilities.makeCompactGrid(selections, //parent
										numOfAttrib, 1,
										5, 5,  //initX, initY
										4, 4); //xPad, yPad
		overallPanel.add(selections);
		JButton okBut = new JButton("OK");
		overallPanel.add(okBut);
		
		this.getContentPane().add(overallPanel);
		this.pack();
		this.validate();					

		mod = in;
		
		okBut.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent e) {
		   	// first get the choices from the dialog
		   	int layerIndex = getSelectedLayerIndex();
			PlayingBoardPanel pbp = PlayingBoardPanel.getInstance();
			MouseHandler h = (MouseHandler)pbp.getCurrHandler();
			if ( h instanceof MultiSelectMouseHandler ){
				MultiSelectMouseHandler mh = (MultiSelectMouseHandler)pbp.getCurrHandler();
				ArrayList listOfElements = mh.getSelectedElements(); 
			
				// generate a change Layer command
				ChangeLayerCommand cmd = new ChangeLayerCommand(layerIndex, listOfElements);
				 cmd.setPlayerName(Players.getSelfName());
				 mod.addCmd(cmd);
				 // outLabel.setText(sb.toString());
			}
			 hide();
		   }
		 });

	}
	
	public int getSelectedLayerIndex() {
		ButtonModel butMod = group.getSelection();
		String action = butMod.getActionCommand();
		int index = Integer.parseInt(action);
		// Debug.out("get LayerName from button press = " + action);
		return ( index );
	}
	
}


